package com.ruoyi.develop.factory.factoryTestZnDetail.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.develop.factory.factoryTestZn.domain.FactoryTestZn;
import com.ruoyi.develop.factory.factoryTestZn.service.IFactoryTestZnService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.factory.factoryTestZnDetail.domain.FactoryTestZnDetail;
import com.ruoyi.develop.factory.factoryTestZnDetail.service.IFactoryTestZnDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出厂试验-镀锌质量-每次明细Controller
 * @author lmm
 * @date 2025-06-07
 */
@RestController
@RequestMapping("/factoryTestZnDetail/factoryTestZnDetail")
public class FactoryTestZnDetailController extends BaseController {

    @Autowired
    private IFactoryTestZnDetailService factoryTestZnDetailService;

    @Autowired
    private IFactoryTestZnService factoryTestZnService;

    /**
     * 查询出厂试验-镀锌质量-每次明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZnDetail:query')")
    @GetMapping("/list")
    public TableDataInfo list(FactoryTestZnDetail entity) {
        startPage();
        List<FactoryTestZnDetail> list = factoryTestZnDetailService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出出厂试验-镀锌质量-每次明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZnDetail:export')")
    @Log(title = "出厂试验-镀锌质量-每次明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FactoryTestZnDetail entity) {
        List<FactoryTestZnDetail> list = factoryTestZnDetailService.selectList(entity);
        ExcelUtil<FactoryTestZnDetail> util = new ExcelUtil<FactoryTestZnDetail>(FactoryTestZnDetail. class);
        util.exportExcel(response, list, "出厂试验-镀锌质量-每次明细数据");
    }

    /**
     * 获取出厂试验-镀锌质量-每次明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZnDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(factoryTestZnDetailService.selectById(id));
    }

    /**
     * 新增出厂试验-镀锌质量-每次明细
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZnDetail:add')")
    @Log(title = "出厂试验-镀锌质量-每次明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FactoryTestZnDetail entity) {
        factoryTestZnDetailService.insert(entity);
        FactoryTestZnDetail params = new FactoryTestZnDetail();
        params.setTestId(entity.getTestId());
        List<FactoryTestZnDetail> list = factoryTestZnDetailService.selectList(params);
        // 计算list 中ZnThick所有数据的平均数
        BigDecimal sum = BigDecimal.ZERO;
        for (FactoryTestZnDetail item : list) {
            sum = sum.add(item.getZnThick());
        }
        BigDecimal avg = sum.divide(new BigDecimal(list.size()), 2, BigDecimal.ROUND_HALF_UP);
        FactoryTestZn factoryTestZn = factoryTestZnService.selectById(entity.getTestId());
        factoryTestZn.setZnThick(avg);
        factoryTestZnService.update(factoryTestZn);

        return toAjax(1);
    }

    /**
     * 修改出厂试验-镀锌质量-每次明细
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZnDetail:edit')")
    @Log(title = "出厂试验-镀锌质量-每次明细", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FactoryTestZnDetail entity) {
        factoryTestZnDetailService.update(entity);
        FactoryTestZnDetail params = new FactoryTestZnDetail();
        List<FactoryTestZnDetail> list = factoryTestZnDetailService.selectList(params);
        // 计算list 中ZnThick所有数据的平均数
        BigDecimal sum = BigDecimal.ZERO;
        for (FactoryTestZnDetail item : list) {
            sum = sum.add(item.getZnThick());
        }
        BigDecimal avg = sum.divide(new BigDecimal(list.size()), 2, BigDecimal.ROUND_HALF_UP);
        FactoryTestZn factoryTestZn = factoryTestZnService.selectById(entity.getTestId());
        factoryTestZn.setZnThick(avg);
        factoryTestZnService.update(factoryTestZn);
        return toAjax(1);
    }

    /**
     * 删除出厂试验-镀锌质量-每次明细
     */
    @PreAuthorize("@ss.hasPermi('develop:FactoryTestZnDetail:remove')")
    @Log(title = "出厂试验-镀锌质量-每次明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(factoryTestZnDetailService.deleteByIds(ids));
    }
}
